package name.jiaosha.air.company.service.impl;

import java.util.List;

import name.jiaosha.air.company.dao.ICarInfoDao;
import name.jiaosha.air.company.dao.ICompanyDao;
import name.jiaosha.air.company.dao.IDepartmentDao;
import name.jiaosha.air.company.dao.IEmployeeDao;
import name.jiaosha.air.company.dao.IStorageDao;
import name.jiaosha.air.company.domain.CarInfo;
import name.jiaosha.air.company.domain.Company;
import name.jiaosha.air.company.domain.Department;
import name.jiaosha.air.company.domain.Employee;
import name.jiaosha.air.company.domain.Storage;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CompanyService implements ICompanyService {

	@Autowired
	private ICompanyDao companyDao;
	@Autowired
	private ICarInfoDao carInfoDao;
	@Autowired
	private IDepartmentDao departmentDao;
	@Autowired
	private IStorageDao storageDao;
	@Autowired
	private IEmployeeDao employeeDao;

	@Override
	public Company getCompanyById(int id) {

		return companyDao.get(id);
	}

	@Override
	public void createCompany(Company company) {
		companyDao.save(company);
	}

	@Override
	public void createCarInfo(CarInfo carInfo) {
		carInfoDao.save(carInfo);
	}

	@Override
	public CarInfo getCarInfoById(int id) {

		return carInfoDao.get(id);
	}

	@Override
	public List<CarInfo> getCarListByComId(Integer comId) {

		String jpql = "SELECT c FROM CarInfo c";

		return carInfoDao.find(jpql);

	}

	@Override
	public void updateCarInfo(CarInfo carInfo) {

		carInfoDao.saveOrUpdate(carInfo);
	}

	@Override
	public List<Department> getDepartmentByComId(Integer comId) {

		String jpql = "SELECT d FROM Department d";
		return departmentDao.find(jpql);
	}

	@Override
	public Department getDepartmentById(Integer id) {

		return departmentDao.get(id);
	}

	@Override
	public void createDepartment(Department department) {
		departmentDao.save(department);

	}

	@Override
	public void updateDepartment(Department department) {
		departmentDao.saveOrUpdate(department);

	}

	@Override
	public void createEmployee(Employee employee) {

		employeeDao.save(employee);

	}

	@Override
	public void updateEmployee(Employee employee) {

		employeeDao.saveOrUpdate(employee);

	}

	
	
	@Override
	public Employee getEmployeeById(int id) {
		
		return employeeDao.get(id);
	}

	@Override
	public List<Employee> getEmployeeByComId(Integer comId) {
		String jpql = "SELECT e FROM Employee e WHERE e.company.id=?1";
		return employeeDao.find(jpql,comId);
	}

	@Override
	public List<Storage> getStorageByComId(Integer comId) {
		String jpql = "SELECT s FROM Storage s";
		return storageDao.find(jpql);
	}

	@Override
	public Storage getStorageById(Integer id) {

		return storageDao.get(id);
	}

	@Override
	public void createStorage(Storage storage) {
		storageDao.save(storage);

	}

	@Override
	public void updateStorage(Storage storage) {
		storageDao.saveOrUpdate(storage);

	}

}
